package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbSpecificationMapper;
import com.dongdongshop.mapper.TbSpecificationOptionMapper;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.pojo.TbSpecificationOptionExample;
import com.dongdongshop.service.TbSpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@com.alibaba.dubbo.config.annotation.Service
public class TbSpecificationServiceImpl implements TbSpecificationService {

    @Autowired
    private TbSpecificationMapper tbSpecificationMapper;
    @Autowired
    private TbSpecificationOptionMapper tbSpecificationOptionMapper;

    @Override
    public List<TbSpecification> select(TbSpecification tbSpecification) {
        List<TbSpecification> list =  tbSpecificationMapper.selectByName(tbSpecification);
        return list;
    }

    @Override
    public Boolean insertinto(List<TbSpecificationOption> list, TbSpecification specNames) {
        int i = tbSpecificationMapper.insrtByname(specNames);
        Long id = specNames.getId();
        System.out.println(id);
        int insertinto = tbSpecificationOptionMapper.insertinto(list, id);
        return insertinto>0;
    }

    @Override
    public Boolean delsete(Long id) {
        int i = tbSpecificationMapper.deleteByPrimaryKey(id);
        tbSpecificationMapper.deleteByid(id);
        return i>0;
    }

    @Override
    public TbSpecification selectByid(Long id) {
        TbSpecification tbSpecification = tbSpecificationMapper.selectByPrimaryKey(id);
        List<TbSpecificationOption> tbSpecificationOption = tbSpecificationOptionMapper.selectById(id);
        return tbSpecification;
    }
    @Override
    public List<TbSpecificationOption> selectByids(Long id) {
        TbSpecification tbSpecification = tbSpecificationMapper.selectByPrimaryKey(id);
        List<TbSpecificationOption> tbSpecificationOption = tbSpecificationOptionMapper.selectById(id);
        return tbSpecificationOption;
    }

    @Override
    public Boolean update(TbSpecification tbSpecification, List<TbSpecificationOption> list) {
        TbSpecificationOptionExample tbSpecificationOptionExample = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = tbSpecificationOptionExample.createCriteria();
        criteria.andSpecIdEqualTo(tbSpecification.getId());
        tbSpecificationOptionMapper.deleteByExample(tbSpecificationOptionExample);
        int insertinto = tbSpecificationOptionMapper.insertinto(list, tbSpecification.getId());
        return insertinto>0;
    }

    @Override
    public List<TbSpecificationOption> selectBySpecId(Long specId) {
        TbSpecificationOptionExample tbSpecificationOptionExample = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = tbSpecificationOptionExample.createCriteria();
        criteria.andSpecIdEqualTo(specId);
        List<TbSpecificationOption> tbSpecificationOptions = tbSpecificationOptionMapper.selectByExample(tbSpecificationOptionExample);
        return tbSpecificationOptions;

    }

}
